/*
 * 此类修改自org.springframework.web.context.ContextLoaderListener
 * ------------------------------------------------------------------------------------
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.TTBook.utils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.ContextLoaderServlet;
import org.springframework.web.context.WebApplicationContext;

/**
 * 此类修改自{@link org.springframework.web.context.ContextLoaderListener} .<br>
 * Bootstrap listener to start up Spring's root {@link WebApplicationContext}.
 * Simply delegates to {@link ContextLoader}.
 * 
 * <p>
 * This listener should be registered after
 * {@link org.springframework.web.util.Log4jConfigListener} in
 * <code>web.xml</code>, if the latter is used.
 * 
 * <p>
 * For Servlet 2.2 containers and Servlet 2.3 ones that do not initalize
 * listeners before servlets, use {@link ContextLoaderServlet}. See the
 * latter's Javadoc for details.
 * 
 * @author wenyf
 * @since 09/29/08
 * @see ContextLoaderServlet
 * @see org.springframework.web.util.Log4jConfigListener
 * @see org.springframework.web.context.ContextLoaderListener
 */
public class SpringContextLoaderListener implements ServletContextListener {
	private ContextLoader contextLoader;

	/**
	 * 应用上下
	 */
	private static WebApplicationContext context = null;

	/**
	 * Initialize the root web application context.
	 */
	public void contextInitialized(ServletContextEvent event) {
		this.contextLoader = createContextLoader();
		context = this.contextLoader.initWebApplicationContext(event
				.getServletContext());
	}

	/**
	 * Close the root web application context.
	 */
	public void contextDestroyed(ServletContextEvent event) {
		if (this.contextLoader != null) {
			this.contextLoader.closeWebApplicationContext(event
					.getServletContext());
		}
	}

	/**
	 * Create the ContextLoader to use. Can be overridden in subclasses.
	 * 
	 * @return the new ContextLoader
	 */
	protected ContextLoader createContextLoader() {
		return new ContextLoader();
	}

	/**
	 * Return the ContextLoader used by this listener.
	 * 
	 * @return the current ContextLoader
	 */
	public ContextLoader getContextLoader() {
		return this.contextLoader;
	}

	/**
	 * 取得Spring Web应用上下�?
	 * 
	 * @return <code>org.springframework.web.context.WebApplicationContext</code>
	 *         Spring Web应用上下�?
	 * @throws RuntimeException
	 *             如果<code>SpringContextLoaderListener</code>未被启用(未在web.xml中配�?
	 */
	public static WebApplicationContext getSpringWebApplicationContext() {
		if (context == null) {
			throw new RuntimeException(
					"SpringContextLoaderListener has not been bootstraped");
		}

		return context;
	}
}
